package cc.minos.question.plugins
{
	import cc.minos.console.Console;
	import cc.minos.utils.StringUtil;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	
	/**
	 * ...
	 * 拖动题
	 * @author Minos
	 */
	public class DragPlugin extends ChoicePlugin
	{
		public static const VERSION : Number = 1.0;
		public static const API : Number = 2.0;
		
		protected var hits : Array;
		protected var hitedAry : Array;
		protected var points : Array;
		protected var currentChoice : MovieClip;
		
		private var hreg:RegExp = /\bh_[0-9]{1,2}_mc\b/;
		public function DragPlugin()
		{
			this.propName = "DragPlugin";
			//this.acronym = "drag";
		}
		
		override protected function init() : void
		{
			super.init();
			hits = [];
			points = [];
			hitedAry = [];
			
			for each( var o:Object in skin ) {
				if ( o && o is Sprite && hreg.test( o.name ) )
				{
					hits[ getIndex( o.name ) ] = o;
				}
			}
			
			//save choices default position
			for ( var i : int = 0 ; i < options ; i++ )
			{
				points.push( new Point( choices[ i ].x , choices[ i ].y ) );
			}
			
		}
		
		private function getTextFiled( s:Sprite ):TextField
		{
			return (s.getChildByName('txt') as TextField);
		}
		
		private function onChoicesUp( e : MouseEvent ) : void
		{
			var index : int = getIndex( currentChoice.name );
			currentChoice.stopDrag();
			currentChoice.stage.removeEventListener( MouseEvent.MOUSE_UP , onChoicesUp );
			//check hit
			for each ( var i : DisplayObject in hits )
			{
				if ( i.hitTestObject( currentChoice ) )
				{
					hitedAry[ getIndex( i.name ) ] = index;
					break;
				}
			}
			currentChoice = null;
			//
			for ( var j : int = 0 ; j < choices.length ; j++ )
			{
				var m : int = hitedAry.indexOf( getIndex( choices[ j ].name ) );
				if ( m != -1 )
				{
					choices[ j ].x = hits[ m ].x;
					choices[ j ].y = hits[ m ].y;
				}
				else
				{
					choices[ j ].x = points[ j ].x;
					choices[ j ].y = points[ j ].y;
				}
			}
		}
		
		override protected function update( mc : MovieClip = null ) : void
		{
			super.update( mc );
			mc.stage.addEventListener( MouseEvent.MOUSE_UP , onChoicesUp );
			mc.startDrag();
			skin.addChild( mc );
			currentChoice = mc;
			if ( hitedAry.indexOf( getIndex( currentChoice.name ) ) != -1 )
			{
				hitedAry[ hitedAry.indexOf( getIndex( currentChoice.name ) ) ] = null;
			}
		}
		
		override public function check() : Boolean
		{
			for ( var i : int = 0 , len : int = answer.length ; i < len ; i++ )
			{
				if ( hitedAry[ i ] == null || !_check( i ) )
				{
					return false;
				}
			}
			return true;
		}
		
		override public function reset() : void
		{
			super.reset();
			hitedAry = [];
			for ( var i : int = 0 , len : int = choices.length ; i < len ; i++ )
			{
				choices[ i ].x = points[ i ].x;
				choices[ i ].y = points[ i ].y;
			}
		}
		
		/**
		 * 根据答案类型检查
		 * @param	i
		 */
		protected function _check( i:int ):Boolean
		{
			var txt:String = choiceSigns[hitedAry[ i ]];
			var ans:Object = answer[ i ];
			var b:Boolean = true;
			txt = StringUtil.trim( txt );
			
			if ( ans is Array )
			{
				if (( ans as Array ).indexOf( txt ) == -1 )
					b = false;
			}
			else
			{
				if ( StringUtil.trim( ans.toString() ) != txt )
					b = false;
			}
			return b;
		}
		
		override public function get score():Number 
		{
			_score = 0;
			if ( checkAll ) {
				return check() ? tScore : 0;
			}
			
			for ( var i:int = 0 , len:int = answer.length; i < len; i++ ) {
				if ( hitedAry[ i ] != null && _check( i ) )
				{
					_score += tScore / len;
				}else {
					if ( hitedAry[ i ] != null ) 
						Console.log( '答案错误: ' + choiceSigns[ hitedAry[ i ] ] + ' - ' + answer[ i ] , this , 1 );
				}
			}
			
			return _score;
		}
	
	}

}